package com.chain.BrotherExchange.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.TimeCount;
import com.chain.BrotherExchange.viewmodel.RetrievePasswordViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import butterknife.Bind;

/**
 * 找回密码
 */
public class ForgetPasswordActivity extends BaseActivity {

    @Bind(R.id.mobile_phone_edit) EditText mobilePhoneEdit;
    @Bind(R.id.send_text) TextView mSendText;
    @Bind(R.id.code_edit) EditText mCodeEdit;
    @Bind(R.id.new_password_edit) EditText newPasswordEdit;
    @Bind(R.id.affirm_new_password_edit) EditText affirmNewPasswordEdit;
    @Bind(R.id.affirm_button) Button affirmButton;

    private RetrievePasswordViewModel mViewModel;

    public static void launch(TextView view) {
        Activity activity = (Activity) view.getContext();
        ActivityCompat.startActivityForResult(
                activity,
                new Intent(activity, ForgetPasswordActivity.class),
                C.REQ_MOBILE_CODE,
                ActivityOptionsCompat.makeSceneTransitionAnimation(activity, view,
                        activity.getString(R.string.transition_name_mobile_number)).toBundle()
        );
    }

    @Override
    protected int getLayoutID() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        return R.layout.activity_retrieve_password;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_retrieve_password);

        mViewModel = ViewModelProviders.of(this).get(RetrievePasswordViewModel.class);
        mSendText.setOnClickListener(v -> sendMessage());
        affirmButton.setOnClickListener(v -> retrievePassword());
    }

    private void retrievePassword() {
        String code = mCodeEdit.getText().toString();
        String mobilePhone = mobilePhoneEdit.getText().toString();
        String newPassword = newPasswordEdit.getText().toString();
        String affirmNewPassword = affirmNewPasswordEdit.getText().toString();

        mViewModel.retrievePassword(mobilePhone, newPassword, affirmNewPassword, code)
                .observe(this, resource -> {
                    if (resource.state == Resource.Status.LOADING) {
                        Androids.showProgressMessage(this, R.string.resetting_password);
                    } else {
                        Androids.dismissProgressMessage();
                        Androids.makeText(resource.message);
                        if (resource.state == Resource.Status.SUCCESS) {
                            setResult(RESULT_OK,new Intent().putExtra(C.EXTRA_MOBILE, mobilePhone));
                            ActivityCompat.finishAfterTransition(this);
                        }
                    }
                });
    }

    private void sendMessage() {
        String mobilePhone = mobilePhoneEdit.getText().toString();
        mViewModel.sendMessage(mobilePhone).observe(this, resource -> {
            if (resource.state == Resource.Status.LOADING) {
                Androids.showProgressMessage(this, R.string.sending_verification_code);
            } else {
                Androids.dismissProgressMessage();
                Androids.makeText(resource.message);

                if (resource.state == Resource.Status.SUCCESS) {
                    new TimeCount(mSendText).start();
                }
            }
        });
    }

}
